/*
 * FxBattleLauncher.fx
 *
 * Created on Jul 22, 2009, 10:14:52 PM
 */

package fxbattle.launcher;

import fxbattle.client.model.BattleMap;
import fxbattle.fxgui.SocketHexMap;
import fxbattle.client.coms.BattleMapSocketUpdater;
import fxbattle.client.model.Player;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.Tile;
import javafx.scene.control.TextBox;
import javafx.scene.control.Label;
import javafx.geometry.VPos;
import javafx.geometry.HPos;
import javafx.scene.paint.Color;
import javafx.scene.effect.DropShadow;
import java.lang.NumberFormatException;
import javafx.scene.transform.Scale;
import java.io.IOException;
import fxbattle.server.ServerConfigGUI;

/**
 * @author spina
 */

var errorLabel = Label { textFill: Color.RED transforms: [Scale.scale(1.5, 1.5)]}

var hostName = TextBox {
        text: "localhost"
        columns: 20
        selectOnFocus: true
    }

var hostNumber = TextBox {
        text: "4321"
        columns: 20
        selectOnFocus: true
    }

var playerName = TextBox {
        text: "Your Name"
        columns: 20
        selectOnFocus: true
    }



var hostControlTile: Tile = Tile{columns: 1 content: [hostName, hostNumber, playerName]};

var clientStartButton: Button = Button {
    text: "Start Client"
    width: bind clientConfigBox.width
    action: function() {
        println("Start Client");
        def panelheight = 450;
        def panelwidth = 500;
        var map_var:BattleMap = new BattleMap();
        var hostNum: Integer;
        try {
            hostNum = Integer.valueOf(hostNumber.text);
        } catch (e: NumberFormatException) {
            errorLabel.text = "Couldn't convert {hostNumber.text} to a number.";
            return
        }
        var updater:BattleMapSocketUpdater = new BattleMapSocketUpdater(map_var, hostName.text, hostNum);
        println("Waiting for a player");
        
        var player:Player;
        try {
            player = updater.requestToPlay(playerName.text);
        } catch (e: IOException){
            errorLabel.text = e.getMessage();
            return;
        }
        println("Got Player {player.getName()}");
        updater.retrieveInitialState();
        println("Initial State Retrieved. Beginning Listening for Updates");
        updater.beginListening();
        println("Listening For Updates. Ready to display GUI.");
        myStage.scene = Scene {
            fill : Color.WHITE
            content : [SocketHexMap{
                    player: player
                    updater: updater
                    map : map_var
                    effect: DropShadow {
                            offsetX: 10
                            offsetY: 7
                            color: Color.BLACK
                            radius: 25
                        }
                }]
            }
        myStage.width = myStage.scene.width;
        myStage.height = myStage.scene.height;
    }
}

var clientConfigBox = VBox {
    content: [
        HBox { content: [
            Tile{columns: 1 tileHeight: 5 // bind hostControlTile.tileHeight
                 content: [Label { text: "Host Name: " hpos: HPos.RIGHT vpos: VPos.CENTER },
                           Label { text: "Port Number: " hpos: HPos.RIGHT vpos: VPos.CENTER },
                           Label { text: "Player Name: " hpos: HPos.RIGHT vpos: VPos.CENTER}]},
            hostControlTile]},
        clientStartButton
        ]
}

var serverStartButton = Button {
    text: "Start Server"
    width: bind serverConfigBox.width
    action: function() {
        println("Start Server");
        var portNum: Integer;
        try {
            portNum = Integer.valueOf(serverPortNumber.text)
        } catch (e: NumberFormatException){
            errorLabel.text = "Couldn't convert port {serverPortNumber.text} to a number";
            return;
        }
        //TODO this could make the server start button do something =)
//        ServerLauncher.main(String[]{});
        var gui: ServerConfigGUI;
        gui = new ServerConfigGUI();
        gui.setServerPort(portNum);
        gui.setVisible(true);
        //fxbattle.server.gameserver.main(String[]{});
    }
}

var serverPortNumber = TextBox {
        text: "4321"
        columns: 8
        selectOnFocus: true
    }

var thisHostName = Label{ height: bind serverPortNumber.height vpos: VPos.CENTER }
try {
    thisHostName.text = {java.net.InetAddress.getLocalHost().getHostName()};
} catch (e: java.net.UnknownHostException){
    thisHostName.text = "UnknownHostName (See Below)";
    errorLabel.text = e.getMessage();
}

var serverConfigLabels = Tile{columns: 1 tileHeight: 5 //bind serverPortNumber.height
     content: [Label { hpos: HPos.RIGHT vpos: VPos.CENTER},
               Label { text: "Your Host Name: " hpos: HPos.RIGHT vpos: VPos.CENTER },
               Label { text: "Port Number: " hpos: HPos.RIGHT vpos: VPos.CENTER }]}

var serverConfigBox: VBox = VBox {content: [HBox {content: [
            serverConfigLabels, 
            Tile{columns: 1 tileHeight: 5// bind serverPortNumber.height 
                content: [ Label{}, thisHostName, serverPortNumber]}]}, serverStartButton, ]}

var myStage = Stage {
    title: "Run FxBattle"
    width: 450
    height: 250
    scene: Scene {
        content: [
            VBox{ content: [HBox{ content: [clientConfigBox, serverConfigBox] }, errorLabel] }
        ]
    }
}